/**
 * 
 */
package com.umpay.utils.mail;

import java.io.File;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.umpay.util.Base64;

/**
 * 发送邮件
 * 
 * 
 */
public abstract class MailSender {

    public static final String MIME_TYPE = "GB18030";

    protected String serverHost;
    protected int serverPort;
    protected boolean isAuthen;
    protected String userName;
    protected String userPwd;
    // ====================================
    protected String from;// 发送者邮箱
    protected String to;// 设置收件人
    protected String cc;// 抄送人
    // =======================================
    protected String subject;// 邮件主题
    protected String txtContent;// 邮件内容
    protected List<File> attachFile;// 附件

    // ===============================================================
    /**
     * 根据不同的协议生成不同的属性配置
     * 
     * @return
     */
    public abstract Properties getProperties();

    public void sendMail() throws MessagingException {
    Authenticator auth = new Authenticator(this.userName,this.userPwd);
	Session session = Session.getInstance(getProperties(),auth);
	Message msg = createMessage(session);
	Transport.send(msg);
    }

    protected Message createMessage(Session session) throws MessagingException {
	Message msg = new MimeMessage(session);
	// 设置发件人
	msg.setFrom(new InternetAddress(from));
	// 设置收件人
	msg.setRecipients(RecipientType.TO, InternetAddress.parse(to));
	if (cc != null) {// 设置抄送人
	    msg.setRecipients(RecipientType.CC, InternetAddress.parse(cc));
	}
	// 设置主题
	msg.setSubject(subject == null ? "No subject" : subject);

	// 设置正文
	MimeMultipart part = new MimeMultipart();
	// --文本正文
	MimeBodyPart mbp = new MimeBodyPart();
	mbp.setText(txtContent);
	part.addBodyPart(mbp);
	// --添加附件
	if (attachFile != null) {
	    for (File file : attachFile) {
		MimeBodyPart tmp = new MimeBodyPart();
		FileDataSource ds = new FileDataSource(file);
		tmp.setDataHandler(new DataHandler(ds));
		String name=file.getName();
		tmp.setFileName("=?GBK?B?"+Base64.encode(name.getBytes())+"?=");
		part.addBodyPart(tmp);
	    }
	}
	msg.setContent(part);
	return msg;
    }

    public String getServerHost() {
	return serverHost;
    }

    public void setServerHost(String serverHost) {
	this.serverHost = serverHost;
    }

    public int getServerPort() {
	return serverPort;
    }

    public void setServerPort(int serverPort) {
	this.serverPort = serverPort;
    }

    public boolean isAuthen() {
	return isAuthen;
    }

    public void setAuthen(boolean isAuthen) {
	this.isAuthen = isAuthen;
    }

    public String getUserName() {
	return userName;
    }

    public void setUserName(String userName) {
	this.userName = userName;
    }

    public String getUserPwd() {
	return userPwd;
    }

    public void setUserPwd(String userPwd) {
	this.userPwd = userPwd;
    }

    public String getFrom() {
	return from;
    }

    public void setFrom(String from) {
	this.from = from;
    }

    public String getTo() {
	return to;
    }

    public void setTo(String to) {
	this.to = to;
    }

    public String getCc() {
	return cc;
    }

    public void setCc(String cc) {
	this.cc = cc;
    }

    public String getSubject() {
	return subject;
    }

    public void setSubject(String subject) {
	this.subject = subject;
    }

    public String getTxtContent() {
	return txtContent;
    }

    public void setTxtContent(String txtContent) {
	this.txtContent = txtContent;
    }

    public List<File> getAttachFile() {
	return attachFile;
    }

    public void setAttachFile(List<File> attachFile) {
	this.attachFile = attachFile;
    }

}
